<template>
  <div class="app-container-custom">
    <el-form :inline="true" :model="queryParams" ref="queryForm" size="small" label-width="0" class="demo-form-inline">
      <el-form-item prop="costNo">
        <el-input
          v-model="queryParams.costNo"
          placeholder="费用单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="chargeId">
        <el-select v-model="queryParams.chargeId" placeholder="收费项目" clearable size="small">
          <el-option
            v-for="dict in itemOptions"
            :key="dict.chargeId"
            :label="dict.chargeName"
            :value="dict.chargeId"
          />
        </el-select>
      </el-form-item>

      <el-form-item style="width: 100px;" prop="status">
        <el-select v-model="queryParams.status" placeholder="费用状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="receivableTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="应收日期开始"
          end-placeholder="应收日期结束">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="mini" :icon="queryParams.params.searchQuery?'el-icon-loading':''"
                   @click="$refs.queryRef.show()">高级查询
        </el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="ruleCostList"
      @selection-change="handleSelectionChange"
      border
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="单据号" align="left" prop="costNo" width="120"/>
      <el-table-column label="房屋名称" align="left" width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.roomName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="left" prop="ownerName" width="100"/>
      <el-table-column label="收费类型" align="left" prop="projectId" width="120" :formatter="projectFormat"/>
      <el-table-column label="收费项目" align="left" prop="chargeId" width="120" :formatter="itemFormat"/>
      <el-table-column label="收费标准" align="left" prop="normNames" width="120"/>
      <el-table-column label="费用期间" align="center" prop="fiscalPeriod" width="100"/>
      <el-table-column label="计费开始日期" align="center" prop="beginTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计费结束日期" align="center" prop="finishTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="prices" width="60"/>
      <!--<el-table-column label="金额" align="center" prop="moneys"  width="100"/>-->
      <el-table-column label="应收金额" align="center" prop="receivableMoney" width="100"/>
      <el-table-column label="已收金额" align="center" prop="haveMoney" width="100"/>
      <el-table-column label="优惠金额" align="center" prop="specialMoney" width="100"/>
      <!--<el-table-column label="优惠后金额" align="center" prop="queenMoney"  width="120"/>-->
      <el-table-column label="冲抵金额" align="center" prop="amountAmount" width="100"/>
      <el-table-column label="收全日期" align="center" prop="fullReceiptDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fullReceiptDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80" :formatter="statusFormat"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <l-super-query ref="queryRef" keyName="PayDemand" :columns="queryColumns" @searchQuery="searchQuery"
                   @resetting="queryParams.params.searchQuery=null"/>
  </div>
</template>

<script>
  import {
    listRuleCost,
    getRuleCost,
    delRuleCost,
    addRuleCost,
    updateRuleCost,
    exportRuleCost,
    createRuleCost
  } from "@/api/payfee/ruleCost";
  import {listItem} from "@/api/payfee/item";
  import {listProject} from "@/api/payfee/project";

  export default {
    name: "RuleCost",
    data() {
      return {
        toggleSearchStatus: false,
        sideshow: true,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 常规费用单表格数据
        ruleCostList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          costNo: null,
          roomId: null,
          deptId: null,
          ownerId: null,
          projectId: null,
          chargeId: null,
          normId: null,
          roomNormId: null,
          receivableTime: null,
          beginTime: null,
          finishTime: null,
          prices: null,
          moneys: null,
          specialMoney: null,
          queenMoney: null,
          receivableMoney: null,
          haveMoney: null,
          costType: null,
          fullReceiptDate: null,
          status: null,
          params: {}
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        currentNode: {},
        //收费项目字典
        itemOptions: [],
        //收费类型字典
        projectOptions: [],
        currentOptions: [],
        receivableTime: [], //应收日期
        queryColumns: [
          {title: '费用单号', key: 'costNo', typeCode: '', type: 'input', value: ''},
          {title: '客户名称', key: 'owner_name', type: 'input', value: '', alias: 'tor'},
          {title: '计费开始时间', key: 'begin_time', type: 'date', value: ''},
          {title: '计费结束时间', key: 'finish_time', type: 'date', value: ''},
        ]
      };
    },
    created() {
      this.getDicts("charge_status").then(response => {
        this.statusOptions = response.data;
      });
      listItem().then(res => {
        this.itemOptions = res.rows;
      })
      listProject().then(res => {
        this.projectOptions = res.rows;
      })
    },
    methods: {
      init(options) {
        this.currentOptions = options
        this.getList();
      },
      /** 查询常规费用单列表 */
      getList() {
        this.loading = true;
        this.queryParams.roomId = this.currentOptions.roomIds || "-1"
        let queryParams = this.addDateRange(this.queryParams, this.receivableTime, "receivableTime")
        listRuleCost(queryParams).then(response => {
          this.ruleCostList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 收费项目字典翻译
      itemFormat(row, column) {
        let index = this.itemOptions.findIndex(x => x.chargeId == row.chargeId);
        if (index != -1) {
          return this.itemOptions[index].chargeName
        }
        return '';
      },
      // 收费类型字典翻译
      projectFormat(row, column) {
        let index = this.projectOptions.findIndex(x => x.projectId == row.projectId);
        if (index != -1) {
          return this.projectOptions[index].projectType
        }
        return '';
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          costId: null,
          costNo: null,
          roomId: null,
          deptId: null,
          ownerId: null,
          projectId: null,
          chargeId: null,
          normId: null,
          roomNormId: null,
          receivableTime: null,
          beginTime: null,
          finishTime: null,
          prices: null,
          moneys: null,
          specialMoney: null,
          queenMoney: null,
          receivableMoney: null,
          haveMoney: null,
          costType: null,
          fullReceiptDate: null,
          status: "0",
          ancestors: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.params = {}
        this.receivableTime = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.costId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加常规费用单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const costId = row.costId || this.ids
        getRuleCost(costId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改常规费用单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.costId != null) {
              updateRuleCost(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRuleCost(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const costIds = row.costId || this.ids;
        this.$confirm('是否确认删除常规费用单编号为"' + costIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delRuleCost(costIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有常规费用单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRuleCost(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      },
      handleNodeClick(node) {
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      handleFirstNodeClick(node) {
        this.currentNode = node;
        this.queryParams.nodeId = node.id;
        this.getList();
      },
      handleCreateRuleCost() {
        let param = {nodeId: this.currentNode.id, modelType: this.currentNode.modelType}
        this.$confirm(`是否创建组织为【${this.currentNode.label}】的费用单`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return createRuleCost(param);
        }).then(() => {
          this.getList();
          this.msgSuccess("费用创建成功");
        }).catch(() => {
        });
      },
      tableRowClassName({row, rowIndex}) {
        if (row.status === '10001') {
          return 'wsq-row';
        } else if (row.status === '10002') {
          return 'ysq-row';
        }
        return '';
      },
      searchQuery(queryParams) {
        this.queryParams.params['searchQuery'] = queryParams
        this.getList(true)
      },
    }
  };
</script>
<style scoped lang="scss">
  ::v-deep .el-table .ysq-row {
    background: #f0f9eb;
  }

  ::v-deep .el-table .wsq-row {
    background: #e642425e;
  }
</style>
